﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using zichan.DAL;
using zichan.Entity;
using MvcPaging;
namespace zichan.BLL
{
    public class UsageLogService
    {
        public static void Add(UsageLog model)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                db.UsageLogs.Add(model);
                db.SaveChanges();
            }
        }
        public static void AddList(List<UsageLog> list)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                db.UsageLogs.AddRange(list);
                db.SaveChanges();
            }
        }
        public static void AddTemList(List<string> ids)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                List<UsageLog> list = new List<UsageLog>();
               foreach(string id in ids)
                {
                    Asset model = AssetService.Get(Int32.Parse(id));
                    UsageLog usageLog = new UsageLog();
                    usageLog.AssetClassId = model.AssetClassId;
                    usageLog.Code = model.Code;
                    usageLog.Time = DateTime.Now;
                    usageLog.Operation = "新增入库";
                    usageLog.AssetId = model.Id;
                    list.Add(usageLog);
                }
                AddList(list);
            }
        }
        public static void AddBaseToId(int assetId)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                Asset model = db.Assets.FirstOrDefault(o => o.Id == assetId);
                UsageLog usageLog = new UsageLog();
                usageLog.AssetClassId = model.AssetClassId;
                usageLog.AssetId = model.Id;
                usageLog.Code = model.Code;
                usageLog.Time = DateTime.Now;
                usageLog.Operation = "完善信息";
                usageLog.Remark = model.Remark;
                db.UsageLogs.Add(usageLog);
                db.SaveChanges();
            }
        }
        public static void AddBaseToId(int assetId,string operation,string remark,int? staff)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                Asset model = db.Assets.FirstOrDefault(o => o.Id == assetId);
                UsageLog usageLog = new UsageLog();
                usageLog.AssetClassId = model.AssetClassId;
                usageLog.AssetId = model.Id;
                usageLog.Code = model.Code;
                usageLog.Time = DateTime.Now;
                usageLog.Operation = operation;
                usageLog.Remark = remark;
                usageLog.StaffId = staff;
                db.UsageLogs.Add(usageLog);
                db.SaveChanges();
            }
        }
        public static IPagedList<UsageLog> GetList(int id, int pageIndex, int pageSize)
        {
            using (ZichanDBContext db = new ZichanDBContext())
            {
                IQueryable<UsageLog> ul = db.UsageLogs.Where(o => o.AssetId == id);
                List<UsageLog> u = ul.OrderByDescending(o => o.Time).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                List<int> assetClassIds = u.Select(o => o.AssetClassId).Distinct().ToList();
                var assetClassNames = db.AsseteClassifies.Where(o => assetClassIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                List<int> assetIds = u.Select(o => o.AssetId).Distinct().ToList();
                var assetNames = db.Assets.Where(o => assetIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                List<int> staffIds = u.Where(o => o.StaffId.HasValue).Select(o => o.StaffId.Value).Distinct().ToList();
                var staffNames = db.Staffs.Where(o => staffIds.Contains(o.Id)).Select(o => new { o.Id, o.Name });
                foreach (UsageLog l in u)
                {
                    l.AssetClassName = assetClassNames.Where(o => o.Id == l.AssetClassId).FirstOrDefault().Name;
                    l.AssetName = assetNames.Where(o => o.Id == l.AssetId).FirstOrDefault().Name;
                    if (l.StaffId.HasValue)
                        l.StaffName = staffNames.Where(o => o.Id == l.StaffId).FirstOrDefault().Name;
                }
                return u.OrderByDescending(o => o.Time).ToList().ToPagedList(pageIndex - 1, pageSize, ul.Count());
            }
        }
    }
}
